package cn.lagou.interceptor;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        String pwd = (String) session.getAttribute("pwd");

        if (StringUtils.pathEquals("admin", username)
                && StringUtils.pathEquals("admin", pwd))
            return true;

        request.setAttribute("msg", "Please to login in!");
//        response.sendRedirect(request.getContextPath() + "/login");
        request.getRequestDispatcher("/login").forward(request, response);
        return false;
    }
}
